var shuju = [];
// var checkedSet = new Set();

layui.use(['table', 'layer'], function () {
    // var table = layui.table;
    var layer = layui.layer;
    var table = layui.table
        , form = layui.form
        , $ = layui.$;


    table.render({
        elem: '#test'
        , url: '/list'
        , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板

        //filter,exports,print
        , defaultToolbar: ['', '', ''
        ]
        , title: '用户数据表'
        , cols: [[
            {type: 'checkbox', fixed: 'left', style: 'height:150px'}
            , {field: 'studentId', title: 'ID', width: 60, fixed: 'left', unresize: true, sort: true}
            , {field: 'name', title: '姓名', width: 90}
            , {field: 'age', title: '年龄', width: 60}
            , {field: 'grade', title: '年级', width: 80}
            , {field: 'department', title: '院系', width: 100}
            , {field: 'major', title: '专业'}
            , {field: 'sex', title: '性别', width: 60}
            , {field: 'nation', title: '民族', width: 60}
            , {field: 'idCard', title: '身份证', width: 120}
            , {field: 'schooling', title: '学制', width: 80}
            , {field: 'phone', title: '手机', width: 120}
            , {
                field: 'picture',
                title: '照片',
                templet: '<div><img  src="{{d.picture}}"  style="width: 100px"/></div>',
                width: 110,
                style: 'height:140px'
            }
            , {field: 'pay', title: '缴费状态', width: 120}
            , {field: 'start', title: '报名状态', width: 120}
            , {field: 'createTime', title: '创建时间'}
            , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 180}
        ]]
        , page: true
        , id: 'test'
        , height: 'full-150'
        , limits: [1, 2, 3, 4, 5, 10, 20, 30, 40, 50]
        , limit: 3
        , done: function (res, curr, count) {


            console.log("done shuju");
            console.log(shuju);
            console.log(res);

            //得到当前页码
            console.log(curr);

            //得到数据总量
            console.log(count);

        }

    });


    table.on('checkbox(test)', function (obj) {

        const checkStatus = table.checkStatus('test'); //idTest 即为基础参数 id 对应的值

        var s = [1, 2, 3, 4];
        console.log(s);
        console.log("checkStatus,data,length,isall"); //获取选中行的数据
        console.log(checkStatus); //获取选中行的数据
        console.log(checkStatus.data) //获取选中行的数据
        console.log(checkStatus.data.length) //获取选中行数量，可作为是否有选中行的条件
        console.log(checkStatus.isAll) //表格是否全选


        for (j = 0; j < checkStatus.data.length; j++) {
            // checkStatus.data[j]
            shuju.push(checkStatus.data[j]);

        }

        console.log("shuju|||") //表格是否全选
        console.log(shuju) //表格是否全选
        console.log("obj.checked.datatype")
        console.log(obj.checked)
        console.log(obj.data)
        //all时上面拿不到数据要        console.log(checkStatus.data)
        console.log(obj.type)


    });


    //头工具栏事件
    table.on('toolbar(test)', function (obj) {

        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'getCheckData':
                var data = checkStatus.data;
                console.log(JSON.stringify(data))

                $.ajax({
                    url: '/deletes',
                    type: 'post',
                    data: JSON.stringify(data),
                    dataType: 'json',
                    contentType: 'application/json;charset=UTF-8',
                    success: function (data) {
                        if (data.code == 0) {
                            $(".layui-laypage-btn").trigger("click");
                        }
                    },
                    error: function (error) {
                        console.log(error)
                    }
                });
                // layer.alert(JSON.stringify(data));
                break;
            case 'getCheckLength':
                var data = checkStatus.data;
                //todo
                var studentId = '';
                for (var i = 0; i < data.length; i++) {
                    studentId += (data[i].studentId + ',')
                }
                studentId = studentId.substring(0, studentId.length - 1);
                console.log("[" + studentId + "]");
                console.log("[" + data.length + "]");
                if (data.length == 0) {
                    layer.msg("请选择");
                    break;
                }
                window.location.href = '/exportSome?studentId=' + studentId;
                break;
            case 'isAll':
                layer.msg(checkStatus.isAll ? '全选' : '未全选');
                break;
            //自定义头工具栏右侧图标 - 提示
            case 'LAYTABLE_TIPS':
                layer.alert('这是工具栏右侧自定义的一个图标按钮');
                break;
        }

    });


    //监听行工具事件
    table.on('tool(test)', function (obj) {

        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('真的删除行么', function (index) {
                obj.del();
                layer.close(index);
                console.log(obj.data);
                console.log(index);

                $.ajax({
                    url: '/deleteone',
                    type: 'get',
                    data: {studentId: obj.data.studentId},
                    dataType: 'json',
                    success: function (data) {
                        if (data.code == 0) {
                            $(".layui-laypage-btn").trigger("click");
                        }
                    },
                    error: function (error) {
                        console.log(error)
                    }
                });

            });
        } else if (obj.event === 'edit') {
            console.log(obj.data);

            layer.open({
                type: 2,
                title: '添加或编辑',
                maxmin: true,
                shadeClose: true, //点击遮罩关w闭层
                area: ['1400px', '350px'],
                content: '/edit?studentId=' + obj.data.studentId,
                cancel: function () {
                    $(".layui-laypage-btn").trigger("click");

                    //return false 开启该代码可禁止点击该按钮关闭
                },
                end: function () {
                    //z在弹出层的按钮提交后
                    // 成功执行 var index = parent.layer.getFrameIndex(window.name);
                    //                         setTimeout(function(){parent.layer.close(index)}, 700);
                    //
                    $(".layui-laypage-btn").trigger("click");

                }
            });


        } else if (obj.event === 'up') {
            // obj.data.studentId;
            layer.open({
                type: 2,
                title: '上传图片',
                maxmin: true,
                shadeClose: true, //点击遮罩关w闭层
                area: ['400px', '400px'],
                content: "/picture?studentId=" + obj.data.studentId,
                cancel: function () {
                    $(".layui-laypage-btn").trigger("click");
                    // window.location.reload();
                    //return false 开启该代码可禁止点击该按钮关闭
                }
            });

        } else if (obj.event === 'delpic') {

        }
    });

});


layui.use('upload', function () {
    var $ = layui.jquery
        , upload = layui.upload;
    //选完文件后不自动上传
    upload.render({
        elem: '#test8'
        , url: '/upExcel' //改成您自己的上传接口
        , auto: false
        , accept: 'file'
        , exts: 'xlsx|xls'
        //,multiple: true
        , bindAction: '#test9'
        , done: function (res) {
            if (res.code == 0) {

                var table = layui.table;
                table.reload('test', {
                    url: '/list'
                    , where: {} //设定异步数据接口的额外参数
                    , page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });

                $('.layui-laypage-btn').trigger("click");
                // 刷新太快这个显示很快刷掉了
                layer.msg(res.msg);
                // setTimeout(function () {
                //     window.location.reload();
                // }, 1000);

            } else {
                layer.msg('上传出错');
            }
            console.log(res)
        }
    });
});
